package com.example.controller;


import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.api.R;
import com.example.common.dto.TFlowInfoNewDto;
import com.example.common.entity.TFlowInfo;
import com.example.common.utils.UploadUtils;
import com.example.service.TFlowInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Date;

@RestController
@RequestMapping("/flow")
public class UploadFileController {

    private static final Logger log = LoggerFactory.getLogger(UploadFileController.class);

    @Autowired
    private TFlowInfoService flowInfoService;

    @PostMapping(value = "/save", produces = "multipart/form-data;charset=UTF-8")
    public R saveFlowInfo(
            @RequestParam("flowName")String flowName,
            @RequestParam("flowCost") Double flowCost,
            @Valid @RequestParam("flowType") String flowType,
            @RequestParam("flowStartTime") String flowStartTime,
            @RequestParam("flowEndTime") String flowEndTime,
            @RequestParam("flowPlanArray") MultipartFile[] flowPlanArray,
            @RequestParam("flowEnforceArray") MultipartFile[] flowEnforceArray,
            HttpServletRequest request) {

        TFlowInfo flowInfo = new TFlowInfo();


        try {
            flowInfo.setFlowType(flowType);
            flowInfo.setFlowName(flowName);
            flowInfo.setFlowCost(BigDecimal.valueOf(flowCost));

            // 创建一个时间点
            System.out.println(Instant.now());
            flowInfo.setFlowStartTime(new SimpleDateFormat("yyyy-MM-dd").parse(flowStartTime));
            flowInfo.setFlowEndTime(new SimpleDateFormat("yyyy-MM-dd").parse(flowEndTime));

        } catch (ParseException e) {
            e.printStackTrace();
        }

        if(flowPlanArray.length > 0){
           flowInfo.setFlowPlanText(new UploadUtils().uploadFile(flowPlanArray, request));
        }
        if(flowEnforceArray.length > 0){
            flowInfo.setFlowEnforceText(new UploadUtils().uploadFile(flowEnforceArray, request));
        }

        flowInfo.setFlowId(IdWorker.get32UUID());
        System.out.println(flowInfo);
        flowInfoService.insert(flowInfo);
        return R.ok("完成!");
    }


    @PostMapping("/saveNew")
    public R saveFlowInfo( @Valid @RequestBody TFlowInfo dto){
        log.info("saveNew参数：{}", dto);
        dto.setFlowId(IdWorker.get32UUID());
        TFlowInfo flowInfo = new TFlowInfo();
        BeanUtils.copyProperties(dto, flowInfo);
        log.info("saveNew参数：{}", flowInfo);

        flowInfoService.insert(flowInfo);

        return R.ok("流程NewDto完成!");
    }


    @PostMapping("/upload")
    public R uploadFile(@RequestParam("files") MultipartFile[] files, HttpServletRequest request){
        return R.ok(new UploadUtils().uploadFile(files, request));
    }

}
